<?php
/**
 * Created by PhpStorm.
 * User: lenovo
 * Date: 2017/11/14
 * Time: 14:28
 */

namespace app\admin\model;

use think\Db;
class CategoryDesc extends Admin
{
    protected $autoWriteTimestamp = false;
    public function add(array $data = [])
    {
        $CategoryValidate = validate('Category');
        if(!$CategoryValidate->scene('add')->check($data)) {
            return info(lang($CategoryValidate->getError()), 4001);
        }
//        var_dump($data);die;
        $this->allowField(true)->save($data);
        if($this->id > 0){
            return info(lang('Add succeed'), 1, '', $this->id);
        }else{
            return info(lang('Add failed') ,0);
        }
    }

    public function edit(array $data = [])
    {
        $CategoryValidate = validate('Category');
        if(!$CategoryValidate->scene('edit')->check($data)) {
            return info(lang($CategoryValidate->getError()), 4001);
        }
//        return 1111;
        $res = $this->allowField(true)->save($data,['id'=>$data['id']]);
        if($res == 1){
            return info(lang('Edit succeed'), 1);
        }else{
            return info(lang('Edit failed'), 0);
        }
    }
    public  function saveDate($data){

        if( isset( $data['id']) && !empty($data['id'])) {
            $result = $this->edit( $data );
        } else {
            $result = $this->add( $data );
        }
        return $result;
    }

}